package ResultModel

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ StudentModel = (*customStudentModel)(nil)

type (
	// StudentModel is an interface to be customized, add more methods here,
	// and implement the added methods in customStudentModel.
	StudentModel interface {
		FindALL(ctx context.Context, CollegeName string) ([]Student, error)
		studentModel
	}

	customStudentModel struct {
		*defaultStudentModel
	}
)

// NewStudentModel returns a model for the database table.
func NewStudentModel(conn sqlx.SqlConn) StudentModel {
	return &customStudentModel{
		defaultStudentModel: newStudentModel(conn),
	}
}

func (m *defaultStudentModel) FindALL(ctx context.Context, CollegeName string) ([]Student, error) {
	query := fmt.Sprintf("select %s from %s where college_name = $1 limit 100", studentRows, m.table)
	var resp []Student
	err := m.conn.QueryRowsCtx(ctx, &resp, query, CollegeName)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}
